package com.ossean.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.model.OpenhubUrl;
import com.ossean.model.GatherProjectsModel;

public interface GatherDao {
	@Select("select * from ${sourceTable} where id >= #{beginId} and id <= #{endId} and update_mark = 0;")
	public List<GatherProjectsModel> selectbatchPrj(@Param("beginId")int beginId, @Param("endId")int endId,@Param("sourceTable") String sourceTable);
	@Select("select max(id) from ${table}")
	public int getMaxId(@Param("table") String table);
	@Select("select min(id) from ${gatherProjectsTableName} where update_mark = 0")
	public int selectLastId(@Param("gatherProjectsTableName")String gatherProjectsTableName);

	//tag表Insert操作
	@Insert("insert ignore into ${table} (name) values (#{name})")
	public void insertTag(@Param("table") String table, @Param("name") String name);
	
	@Select("select max(extracted_time) from ${table}")
	public String selectMaxExtractedTime(@Param("table") String table);
	
	@Select("select extracted_time from ${table} limit 1")
	public Date selectOneExtractedTime(@Param("table") String table);
	
	//根据tag的name查找tag的id
	@Select("select id from ${table} where name=#{name}")
	public int selectTagIdByName(@Param("table") String table, @Param("name") String name);
	
	//根据urlMD5码找到GatherProjectModel对象
	@Select("select * from ${table} where urlMD5=#{urlMD5}")
	public GatherProjectsModel selectGPMByUrlMD5(@Param("table") String table, @Param("urlMD5") String urlMD5);
	
	//根据update_mark，分别取不同状态的项目列表
	@Select("select * from ${table} where update_mark = #{mark}  limit #{size}") 
	public List<GatherProjectsModel> selectGPMListNew(@Param("table") String table,@Param("mark") int mark,@Param("size") int size);

	//根据id取GatherProjectsModel对象
	@Select("select * from ${table} where id=#{id}")
	public GatherProjectsModel selectGPMById(@Param("table") String table, @Param("id") int id);
	
	//查找相同homepage的项目 
	@Select("select * from ${table} where (homepage=#{homepage} or homepage=#{homepage1} or homepage=#{homepage2} or homepage=#{homepage3} or homepage=#{homepage4}) and update_mark = #{mark} and id!=#{id}")
	public List<GatherProjectsModel> selectGPMBySameHomePage(@Param("table") String table, @Param("homepage") String homepage, @Param("homepage1") String homepage1, @Param("homepage2") String homepage2, @Param("homepage3") String homepage3, @Param("homepage4") String homepage4, @Param("id") int id, @Param("mark") int mark);
	
	//查找相同homepage的项目,增量数据
	@Select("select * from ${table} where (homepage=#{homepage} or homepage=#{homepage1} or homepage=#{homepage2} or homepage=#{homepage3} or homepage=#{homepage4}) and id!=#{id}")
	public List<GatherProjectsModel> selectGPMBySameHomePageForIncrease(@Param("table") String table, @Param("homepage") String homepage, @Param("homepage1") String homepage1, @Param("homepage2") String homepage2, @Param("homepage3") String homepage3, @Param("homepage4") String homepage4, @Param("id") int id);

	//查找相同name的项目
	@Select("select * from ${table} where update_mark = #{mark} and name=#{name} and id!=#{id} and source!='github'")
	public List<GatherProjectsModel> selectGPMBySameName(@Param("table") String table, @Param("name") String name, @Param("id") int id,@Param("mark") int mark);
	
	//查找相同name的项目
	@Select("select * from ${table} where name=#{name} and id!=#{id} and source!='github'")
	public List<GatherProjectsModel> selectGPMBySameNameForIncrease(@Param("table") String table, @Param("name") String name, @Param("id") int id);
	
	//更新update_mark字段
	@Select("update ${table} set update_mark=#{update_mark} where id=#{id}")
	public void updateMark(@Param("table") String table, @Param("update_mark") int update_mark, @Param("id") int id);
	
	@Select("select * from ${table}")
	public List<GatherProjectsModel> select_test(@Param("table") String table);

	@Select("select id,url from  ${table} where source=#{source} order by id limit #{offset},#{limit}")
	public List<OpenhubUrl> selectOpenhubPrj(@Param("table") String table,@Param("source") String source,@Param("offset") int offset,@Param("limit") int limit);
	
	@Select("update ${table} set url=#{url} where id=#{id}")
	public void updateOpenhubUrl(@Param("table") String table, @Param("url") String url, @Param("id") int id);
}